Refresh and sort a JList from a Vector

chris (2004-05-19 16:06:21)
7057 views
0 replies
This trick enables me to dig data from a Vector (in this case address objects which carry a name, tel number and email address. I actually want the JList just to show the names sorted alphabetically.

The method clears the listModel which defines the JList data, then the Addresses Vector is iterated through to pull out each address and get it's name into an array. This array is then sorted and once the sort is complete, I iterate through the array and add each name into the listModel - The end result is a brand new sorted listModel for the JList.

    public void refresh(){
        Address found;
        this.listModel.clear();
        
        int numItems = this.getAddressBookSize();
        String[] a = new String[numItems];
        for (int i=0;i<numItems;i++){
            found = (Address)Addresses.get(i);
            a[i] = found.getName();
        }
        /* attempt to sort the array */
        Arrays.sort(a, String.CASE_INSENSITIVE_ORDER);
        for (int i=0;i<numItems;i++) {
            this.listModel.addElement(a[i]);
        }
    }

christo
comment